'use strict';
const { Forbbiden } = require('@utils/http-exception');

module.exports = () => {
  return async (ctx, next) => {
    const method = ctx.request.method;
    const flag = ctx.app.router.stack.filter(item => {
      return item.regexp.test(ctx.request.path) && item.methods.includes(method);
    });
    if (flag.length) {
      await next();
    } else {
      throw new Forbbiden();
    }
  };
};
